#include "filelist.h"

#include <boost\filesystem.hpp>
#include <boost\regex.hpp>

namespace bfs2 = boost::filesystem2;

FileList::FileList(std::string path) : bLocked(true)
{
	boost::filesystem2::path p(path);
	if(!boost::filesystem2::exists(p))
	{
		uCount = 0xffffffff;
		return;
	}

	strDirPath = p.directory_string();

	_FileList.push_back(File());
	uCount = 0;
	RecursiveSearch(path);


	uActualFile = 0;
}

void FileList::RecursiveSearch(std::string spath)
{
	bfs2::path p(spath);
	if(!bfs2::exists(p))
		return;

	bfs2::directory_iterator st(p);
	bfs2::directory_iterator end;

	for(; st != end; st++)
	{
		if(bfs2::is_directory(st->status()))
			RecursiveSearch(st->path().directory_string());
		else if(regex_match(st->path().extension(), boost::regex("\\.(h|c){1}(pp)?")))
		{
			uCount++;
			File file(st->path().directory_string(), uCount);
			_FileList.push_back(file);
		}
	}
}

File& FileList::NextFile()
{
	if(this->uActualFile < this->uCount)
	{
		uActualFile++;
		return this->_FileList[this->uActualFile];
	}
	return this->_FileList[0];
}

File& FileList::PrevFile()
{
	if(this->uActualFile > 1)
	{
		this->uActualFile--;
		return this->_FileList[this->uActualFile];
	}
	return this->_FileList[0];
}

void FileList::ResetState()
{
	this->uActualFile = 0;
}

File& FileList::SearchID(unsigned int n)
{
	return this->_FileList[n];
}